//
//  ViewController.m
//  EKEventStore
//
//  Created by TOPTEAM on 2017/3/1.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//

#import "ViewController.h"
#import <EventKit/EventKit.h>
@interface Event_Model ()

@property (nonatomic ,strong)NSString *title;
@property (nonatomic ,strong)NSString *lastModified;

@end

@implementation Event_Model
-(NSString *)description{
    return [NSString stringWithFormat:@"日期:%@\n时间:%@",_title,_lastModified];
}
@end


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self EKEventStore];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
/*
 * 获取日历事件
 */
-(void)Use_EKEventStore{
    
    
    EKEventStore * eventStore = [[EKEventStore alloc] init];
   
    NSDate* startDate = [ViewController dateFromString: @"2017-01-01 08:08:08"];//事件段，开始时间
    
    NSDate* endDate = [ViewController dateFromString: @"2018-01-01 08:08:08"];//结束时间，取中间
    
    NSPredicate* predicate = [eventStore predicateForEventsWithStartDate:startDate
                              
                                                                 endDate:endDate
                              
                                                               calendars:nil];
    
    NSArray* eventList = [eventStore eventsMatchingPredicate:predicate];//数组里面就是时间段中的EKEvent事件数组
    
    NSLog(@"eventList:<<<<<<<<<<<<%@>>>>>>>>>>>",eventList);

    NSArray<EKEvent *> *events = [eventList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(EKEvent * _Nullable event, NSDictionary<NSString *,id> * _Nullable bindings) {
        return event.calendar.subscribed;
    }]];
    
    NSLog(@"events:<<<<<<<<<<<<%@>>>>>>>>>>>",events);

    
}
-(void)EKEventStore{
    EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if(granted){
            NSDate* startDate = [ViewController dateFromString: @"2017-01"];//事件段，开始时间
            
            NSDate* endDate = [ViewController dateFromString: @"2018-01"];//结束时间，取中间
            
            
            NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
            
            NSArray<EKEvent *> *eventList = [store eventsMatchingPredicate:fetchCalendarEvents];
            NSArray<EKEvent *> *events = [eventList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(EKEvent * _Nullable event, NSDictionary<NSString *,id> * _Nullable bindings) {
                return event.calendar.subscribed;
            }]];

            
            NSLog(@"events:<<<<<<<<<<<<%@>>>>>>>>>>>",events);
            
            
            NSMutableArray * marr_events_title=[[NSMutableArray alloc]init];
            //节日
            for (EKEvent *event in events) {
                if (event.title) {
                    Event_Model * model=[[Event_Model alloc]init];
                    model.title=event.title;
                    model.lastModified=[ViewController stringFromDate:event.startDate];
                    [marr_events_title addObject:model];
                }
            }
            
            NSLog(@"marr_events_title:<<<<<<<<<<<<%@>>>>>>>>>>>",marr_events_title);

            
            
            
        }
    }];
}



//NSDate转NSString
+ (NSString *)stringFromDate:(NSDate *)date
{
    //用于格式化NSDate对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式：zzz表示时区
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];

    //NSDate转NSString
    NSString *DateString = [dateFormatter stringFromDate:date];

    return [DateString substringToIndex:10];
}

//NSString转NSDate  @"2016-06-26 08:08:08"
+ (NSDate *)dateFromString:(NSString *)string
{
    //需要转换的字符串
    NSString *dateString = string;
    //设置转换格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
//    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [formatter setDateFormat:@"yyyy-MM"];

    //NSString转NSDate
    NSDate *date=[formatter dateFromString:dateString];
    return date;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
